# -*- coding: UTF-8 -*-
#
# Copyright 2009, Josef Kyrian <josef.kyrian@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import g
import control
from config import config
import os
import Xlib.display
import Xlib.X
import types


# menu definition
menu = {
	'items' :  [
		{
			'title' : u'MPlayer',
			'command' : None,
			'isVisibleCallback' : lambda: control.isMPlayer(control.getFocusedWindow()),
			'items' : [
				{
					'title' : u'Změnit poměr stran',
					'command' : None,
					'isVisibleCallback' : None,
					'items' : [
						{
							'title' : u'Original',
							'command' : lambda: g.keySimulator.sendString('[F1]'),
							'isVisibleCallback' : None,
							'items' : None,
						},
						{
							'title' : u'4:3',
							'command' : lambda: g.keySimulator.sendString('[F2]'),
							'isVisibleCallback' : None,
							'items' : None,
						},
						{
							'title' : u'16:9',
							'command' : lambda: g.keySimulator.sendString('[F3]'),
							'isVisibleCallback' : None,
							'items' : None,
						},
						{
							'title' : u'1:1',
							'command' : lambda: g.keySimulator.sendString('[F4]'),
							'isVisibleCallback' : None,
							'items' : None,
						},
						{
							'title' : u'0.85:1',
							'command' : lambda: g.keySimulator.sendString('[F5]'),
							'isVisibleCallback' : None,
							'items' : None,
						},
					],
				},
				{
					'title' : u'Změnit roztažení',
					'command' : None,
					'isVisibleCallback' : None,
					'items' : [
						{
							'title' : u'Zvětšit',
							'command' : lambda: g.keySimulator.sendString('e'),
							'isVisibleCallback' : None,
							'items' : None,
						},
						{
							'title' : u'Zmenšit',
							'command' : lambda: g.keySimulator.sendString('w'),
							'isVisibleCallback' : None,
							'items' : None,
						},
						{
							'title' : u'Na celou obrazovku',
							'command' : lambda: g.keySimulator.sendString('[F9]'),
							'isVisibleCallback' : None,
							'items' : None,
						},
					],
				},
				{
					'title' : u'Fullscreen',
					'command' : lambda: g.keySimulator.sendString('f'),
					'isVisibleCallback' : None,
					'items' : None,
				},
				{
					'title' : u'Zobrazit/Skýt titulky',
					'command' : lambda: g.htpcController.hideMainMenu() and g.keySimulator.sendString('v'),
					'isVisibleCallback' : None,
					'items' : None,
				},
				{
					'title' : u'Zobrazit/Skýt OSD',
					'command' : lambda: g.keySimulator.sendString('o'),
					'isVisibleCallback' : None,
					'items' : None,
				},
			],
		},
		{
			'title' : u'MythTV',
			'command' : None,
			'isVisibleCallback' : None,
			'items' : [
				{
					'title' : u'Spustit',
					'command' : lambda: g.htpcController.hideMainMenu() and control.runMythTV(),
					'isVisibleCallback' : lambda: not control.isRunning(config.mythTvProgramName),
					'items' : None,
				},
				{
					'title' : u'Zobrazit menu',
					'command' : lambda: g.htpcController.hideMainMenu() and g.keySimulator.sendString('m'),
					'isVisibleCallback' : lambda: control.isRunning(config.mythTvProgramName),
					'items' : None,
				},
				{
					'title' : u'Zobrazit EPG',
					'command' : lambda: g.htpcController.hideMainMenu() and g.keySimulator.sendString('s'),
					'isVisibleCallback' : lambda: control.isRunning(config.mythTvProgramName),
					'items' : None,
				},
				{
					'title' : u'Restartovat',
					'command' : lambda: g.htpcController.hideMainMenu() and control.restartMythTV(),
					'isVisibleCallback' : lambda: control.isRunning(config.mythTvProgramName),
					'items' : None,
				},
				{
					'title' : u'Restartovat backend',
					'command' : lambda: g.htpcController.hideMainMenu() and control.restartMythTVBackend(),
					'isVisibleCallback' : None,
					'items' : None,
				},
			],
		},
		{
			'title' : u'Fun 1',
			'command' : None,
			'isVisibleCallback' : None,
			'items' : [
				{
					'title' : u'Spustit',
					'command' : lambda: g.htpcController.hideMainMenu() and control.runFun1(),
					'isVisibleCallback' : lambda: not control.isRunning(config.funOneProgramName),
					'items' : None,
				},
				{
					'title' : u'Vypnout',
					'command' : lambda: g.htpcController.hideMainMenu() and control.stopFun1(),
					'isVisibleCallback' : lambda: control.isRunning(config.funOneProgramName),
					'items' : None,
				},
				{
					'title' : u'Restartovat',
					'command' : lambda: g.htpcController.hideMainMenu() and control.restartFun1(),
					'isVisibleCallback' : lambda: control.isRunning(config.funOneProgramName),
					'items' : None,
				},
			],
		},
		{
			'title' : u'Spustit spořič obrazovky',
			'command' : lambda: g.htpcController.hideMainMenu() and control.runScreenSaver(),
			'isVisibleCallback' : None,
			'items' : None,
		},
		{
			'title' : u'Vypnout HTPC',
			'command' : None,
			'isVisibleCallback' : None,
			'items' : [
				{
					'title' : lambda: u'Zrušit odpočet (zbývá %s)' % control.getShutdownRemainingTime(),
					'command' : lambda: g.htpcController.hideMainMenu() and control.cancelShutdownHTPC(),
					'isVisibleCallback' : lambda: g.shutdownRemainingTime != None,
					'items' : None,
				},
				{
					'title' : u'Vypnout za',
					'command' : None,
					'isVisibleCallback' : None,
					'items' : [
						{
							'title' : u'0.5 hodiny',
							'command' : lambda: g.htpcController.hideMainMenu() and control.shutdownHTPC(30 * 60),
							'isVisibleCallback' : None,
							'items' : None,
						},
						{
							'title' : u'1 hodinu',
							'command' : lambda: g.htpcController.hideMainMenu() and control.shutdownHTPC(60 * 60),
							'isVisibleCallback' : None,
							'items' : None,
						},
						{
							'title' : u'2 hodiny',
							'command' : lambda: g.htpcController.hideMainMenu() and control.shutdownHTPC(120 * 60),
							'isVisibleCallback' : None,
							'items' : None,
						},
					],
				},
				{
					'title' : u'Restartovat nyní',
					'command' : lambda: g.htpcController.hideMainMenu() and control.rebootHTPC(),
					'isVisibleCallback' : None,
					'items' : None,
				},
				{
					'title' : u'Vypnout nyní',
					'command' : lambda: g.htpcController.hideMainMenu() and control.shutdownHTPC(),
					'isVisibleCallback' : None,
					'items' : None,
				},
			],
		},
	],
}


def generateMenuItemIds(parentId, menu):
	"""
	Generates menu item ids
	"""
	if (parentId):
		parentId = '%s/' % parentId
	else:
		parentId = ''
	#endif
	for i, item in enumerate(menu):
		menu[i]['id'] = '%s%s' % (parentId, i)
		
		if (menu[i].get('items', [])):
			generateMenuItemIds(menu[i]['id'], menu[i]['items'])
		#endif
	#endfor
#enddef


def getSubmenuItems(menu):
	"""
	Returns submenu items according their visibility
	"""
	menuItems = []
	for item in menu['items']:
		item = item.copy()
		
		visible = True
		if (item['isVisibleCallback']):
			visible = item['isVisibleCallback']()
		#endif
		if (type(item['title']) == types.FunctionType):
			item['title'] = item['title']()
		#endif
		if (visible):
			menuItems.append(item)
		#endif
	#endfor
	return menuItems
#enddef

